home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / COLLECT.PAK / COLLEDOC.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  291 lines

  1. // colledoc.cpp : implementation of the CCollectDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "collect.h"
  15.  
  16. #include "colledoc.h"
  17. #include "resource.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CMyStruct
  26.  
  27. void CMyStruct::FormatMyStruct(CString& str)
  28. {
  29.     str.Format(_T("{%i, %.4f, %s}"), m_int, m_float, (LPCTSTR)m_str);
  30. }
  31.  
  32. void SerializeElements(CArchive& ar, CMyStruct** ppElements, int nCount)
  33. {
  34.     // Since SerializeElements is always called by the framework with nCount=1
  35.     // for a CMap<>, it is a good idea to implement SerializeElement to handle
  36.     // nCount>1, in case you decide to reuse it for a CArray<> with the same
  37.     // element type.
  38.     if (ar.IsStoring())
  39.     {
  40.         for (int i = 0; i < nCount; i++)
  41.         {
  42.             CMyStruct* pMyStruct = *(ppElements + i);
  43.             WORD w = (WORD)pMyStruct->m_int;
  44.             ar << w;
  45.             ar << pMyStruct->m_float;
  46.             ar << pMyStruct->m_str;
  47.             nCount--;
  48.         }
  49.     }
  50.     else
  51.     {
  52.         for (int i = 0; i < nCount; i++)
  53.         {
  54.             CMyStruct* pMyStruct = new CMyStruct;
  55.             *(ppElements + i) = pMyStruct;
  56.             WORD w;
  57.             ar >> w;
  58.             pMyStruct->m_int = w;
  59.             ar >> pMyStruct->m_float;
  60.             ar >> pMyStruct->m_str;
  61.         }
  62.     }
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CMyObject
  67.  
  68. IMPLEMENT_SERIAL(CMyObject, CObject, 0)
  69.  
  70. CMyObject::CMyObject()
  71. {
  72. }
  73.  
  74. CMyObject::~CMyObject()
  75. {
  76. }
  77.  
  78. void CMyObject::FormatMyObject(CString& str)
  79. {
  80.     str.Format(_T("{%i, %.4f, %s}"), m_int, m_float, (LPCTSTR)m_str);
  81. }
  82.  
  83. void CMyObject::Serialize(CArchive& ar)
  84. {
  85.     WORD w;
  86.     if (ar.IsStoring())
  87.     {
  88.         w = (WORD)m_int;
  89.         ar << w;
  90.         ar << m_float;
  91.         ar << m_str;
  92.     }
  93.     else
  94.     {
  95.         ar >> w;
  96.         m_int = w;
  97.         ar >> m_float;
  98.         ar >> m_str;
  99.     }
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CCollectDoc
  104.  
  105. IMPLEMENT_DYNCREATE(CCollectDoc, CDocument)
  106.  
  107. BEGIN_MESSAGE_MAP(CCollectDoc, CDocument)
  108.     //{{AFX_MSG_MAP(CCollectDoc)
  109.     //}}AFX_MSG_MAP
  110. END_MESSAGE_MAP()
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CCollectDoc construction/destruction
  114.  
  115. CCollectDoc::CCollectDoc()
  116. {
  117. }
  118.  
  119. CCollectDoc::~CCollectDoc()
  120. {
  121. }
  122.  
  123. BOOL CCollectDoc::OnNewDocument()
  124. {
  125.     if (!CDocument::OnNewDocument())
  126.         return FALSE;
  127.  
  128.     CString strFirst;
  129.     strFirst.LoadString(IDS_INITIAL_STRING);
  130.     m_stringList.AddTail(strFirst);
  131.  
  132.     CMyStruct* pMyStruct = new CMyStruct();
  133.     pMyStruct->m_int = 1234;
  134.     pMyStruct->m_float = 12.34f;
  135.     pMyStruct->m_str.LoadString(IDS_INITIAL_STRING);
  136.     m_mystructList.AddTail(pMyStruct);
  137.  
  138.     m_intList.AddTail(100);
  139.  
  140.     m_dwArray.Add(100000);
  141.  
  142.     CMyObject* pMyObject = new CMyObject();
  143.     pMyObject->m_int = 5678;
  144.     pMyObject->m_float = 56.78f;
  145.     pMyObject->m_str.LoadString(IDS_INITIAL_STRING);
  146.     m_myobArray.Add(pMyObject);
  147.  
  148.     CPoint pt(10,10);
  149.     m_ptArray.Add(pt);
  150.  
  151.     CString strKey, strValue;
  152.     strKey.LoadString(IDS_INITIAL_KEY);
  153.     strValue.LoadString(IDS_INITIAL_VALUE);
  154.     m_mapStringToString[strKey] = strValue;
  155.  
  156.  
  157.     CMyObject* pMyObject2 = new CMyObject();
  158.     pMyObject2->m_int = 1357;
  159.     pMyObject2->m_float = 13.57f;
  160.     pMyObject2->m_str.LoadString(IDS_INITIAL_STRING);
  161.     m_mapStringToMyObject[strKey] = pMyObject2;
  162.  
  163.     CMyStruct* pMyStruct2 = new CMyStruct();
  164.     pMyStruct2->m_int = 2468;
  165.     pMyStruct2->m_float = 24.68f;
  166.     pMyStruct2->m_str.LoadString(IDS_INITIAL_STRING);
  167.     m_mapDWordToMyStruct[100] = pMyStruct2;
  168.  
  169.     return TRUE;
  170. }
  171.  
  172. void CCollectDoc::DeleteContents()
  173. {
  174.     m_stringList.RemoveAll();
  175.  
  176.     POSITION pos = m_mystructList.GetHeadPosition();
  177.     while (pos != NULL)
  178.     {
  179.         delete m_mystructList.GetNext(pos);
  180.     }
  181.     m_mystructList.RemoveAll();
  182.  
  183.     m_intList.RemoveAll();
  184.  
  185.     m_dwArray.RemoveAll();
  186.  
  187.     for (int n = 0; n < m_myobArray.GetSize(); n++)
  188.     {
  189.         delete m_myobArray[n];
  190.     }
  191.     m_myobArray.RemoveAll();
  192.  
  193.     m_mapStringToString.RemoveAll();
  194.  
  195.     m_ptArray.RemoveAll();
  196.  
  197.     pos = m_mapStringToMyObject.GetStartPosition();
  198.     while (pos != NULL)
  199.     {
  200.         CString str;
  201.         CMyObject* pMyObject;
  202.         m_mapStringToMyObject.GetNextAssoc(pos, str, pMyObject);
  203.         delete pMyObject;
  204.     }
  205.     m_mapStringToMyObject.RemoveAll();
  206.  
  207.     pos = m_mapDWordToMyStruct.GetStartPosition();
  208.     while (pos != NULL)
  209.     {
  210.         DWORD dwKey;
  211.         CMyStruct* pMyStruct;
  212.         m_mapDWordToMyStruct.GetNextAssoc(pos, dwKey, pMyStruct);
  213.         delete pMyStruct;
  214.     }
  215.     m_mapDWordToMyStruct.RemoveAll();
  216. }
  217.  
  218. /////////////////////////////////////////////////////////////////////////////
  219. // CCollectDoc serialization
  220.  
  221. void CCollectDoc::Serialize(CArchive& ar)
  222. {
  223.     POSITION pos;
  224.     WORD nCount;
  225.     WORD w;
  226.  
  227.     m_stringList.Serialize(ar);
  228.  
  229.     if (ar.IsStoring())
  230.     {
  231.         nCount = (WORD)m_mystructList.GetCount();
  232.         ar << nCount;
  233.         pos = m_mystructList.GetHeadPosition();
  234.         while (pos != NULL)
  235.         {
  236.             CMyStruct* pMyStruct = m_mystructList.GetNext(pos);
  237.             w = (WORD)pMyStruct->m_int;
  238.             ar << w;
  239.             ar << pMyStruct->m_float;
  240.             ar << pMyStruct->m_str;
  241.             nCount--;
  242.         }
  243.         ASSERT(nCount == 0);
  244.     }
  245.     else
  246.     {
  247.         ar >> nCount;
  248.         while (nCount-- > 0)
  249.         {
  250.             CMyStruct* pMyStruct = new CMyStruct;
  251.             ar >> w;
  252.             pMyStruct->m_int = w;
  253.             ar >> pMyStruct->m_float;
  254.             ar >> pMyStruct->m_str;
  255.             m_mystructList.AddTail(pMyStruct);
  256.         }
  257.     }
  258.  
  259.     m_intList.Serialize(ar);
  260.  
  261.     m_dwArray.Serialize(ar);
  262.  
  263.     m_myobArray.Serialize(ar);
  264.  
  265.     m_ptArray.Serialize(ar);
  266.  
  267.     m_mapStringToString.Serialize(ar);
  268.  
  269.     m_mapStringToMyObject.Serialize(ar);
  270.  
  271.     m_mapDWordToMyStruct.Serialize(ar);
  272. }
  273.  
  274. /////////////////////////////////////////////////////////////////////////////
  275. // CCollectDoc diagnostics
  276.  
  277. #ifdef _DEBUG
  278. void CCollectDoc::AssertValid() const
  279. {
  280.     CDocument::AssertValid();
  281. }
  282.  
  283. void CCollectDoc::Dump(CDumpContext& dc) const
  284. {
  285.     CDocument::Dump(dc);
  286. }
  287. #endif //_DEBUG
  288.  
  289. /////////////////////////////////////////////////////////////////////////////
  290. // CCollectDoc commands
  291.